﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using CarRentalWeb.AutorentWebServiceReference;

namespace Autorent.Web.handlers
{
    /// <summary>
    /// Summary description for GetCarsHandler
    /// </summary>
    public class GetCarsHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                HttpRequest request = context.Request;
                string carCategory = request.Params["carCategory"].ToString();
                int regionId;
                // get the region ID for request and check for adequacy of received values
                bool result = Int32.TryParse(request.Params["regionId"].ToString(), out regionId);
                if (result && carCategory != string.Empty)
                {
                    AutoRentEntities entity = new AutoRentEntities(new Uri(System.Web.Configuration.WebConfigurationManager.AppSettings["service"]));
                    // missing image field on auto ???
                    var car1 = new { ID = 1, Image = "ZETRCCMD999.JPG", CarName = "Funny car", Description = "two seats, funny cat, smth else" };
                    var car2 = new { ID = 2, Image = "ZETRCDMD999.JPG", CarName = "Funny car", Description = "two seats, funny cat, smth else" };
                    var car3 = new { ID = 3, Image = "ZETRECMN999.JPG", CarName = "Funny car", Description = "two seats, funny cat, smth else" };
                    var car4 = new { ID = 4, Image = "ZETREDAR999.JPG", CarName = "Funny car", Description = "two seats, funny cat, smth else" };
                    var car5 = new { ID = 5, Image = "ZETRECMR999.JPG", CarName = "Funny car", Description = "two seats, funny cat, smth else" };
                    var car6 = new { ID = 6, Image = "ZETRIDAR999.JPG", CarName = "Funny car", Description = "two seats, funny cat, smth else" };
                    var car7 = new { ID = 7, Image = "ZETRIDMR999.JPG", CarName = "Funny car", Description = "two seats, funny cat, smth else" };
                    var car8 = new { ID = 8, Image = "ZETRICMR999.JPG", CarName = "Funny car", Description = "two seats, funny cat, smth else" };
                    var list = new[] { car1, car2, car3, car4, car5, car6, car7, car8 };
                    StringBuilder sb = new StringBuilder();
                    foreach (var car in list)
                    {
                        sb.Append("<div class = 'carContainer' id='");
                        sb.Append(car.ID);
                        sb.Append("'><div class='carImg'><img src='./img/vehicles/");
                        sb.Append(car.Image);
                        sb.Append("' alt='");
                        sb.Append(car.CarName);
                        sb.Append("'></div><span>" + car.CarName + "</span><br/>");
                        sb.Append("<p>" + car.Description + "</p></div>");
                    }
                    context.Response.Write(sb.ToString());
                }
                else
                {
                    context.Response.Write("<H3>Error occured while processing request</H3>");
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message.ToString());
                System.Diagnostics.Trace.WriteLine(ex.Message + " " + DateTime.Now.ToString());
            }
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}